package com.alibaba.controllers;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.entity.Users;
import com.alibaba.repository.UsersRepository;

@RequestMapping("/Home")
@RestController
public class HomeController {
	@Autowired//自动装配
	private UsersRepository usersRepository;
	private Object Result;

	//查询所有的用户数据
	@RequestMapping("/getAll")
	public List<Users> getAll() {
		List<Users> listUsers = (List<Users>) usersRepository.findAll();
		return listUsers;
	}

	@RequestMapping("/insert")
	public Users insert(){
		Users users = new Users();
		users.setName("lisi");
		users.setPassword("zxcv");
		users.setAge(23);
		users.setCity("上海");
		users.setAddress("上海市金桥区");
		try {
			Users result = usersRepository.save(users);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			boolean b = true;
		}
		return null;
	}

	@RequestMapping("/delete")
	public Optional<Users> delete(){
		usersRepository.deleteById(4);
		Optional<Users> users = usersRepository.findById(4);
		return users;
	}


}
